<template>
  <div>
    <el-form label-width="80px" ref="form" :rules="rules" :model="form">
      <el-form-item label="标题:" prop="news_title">
        <el-input placeholder="请输入新闻标题" v-model="form.news_title"></el-input>
      </el-form-item>
      <el-form-item label="图片:" prop="news_img">
        <el-upload
          class="avatar-uploader"
          action="http://127.0.0.1:9000/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="form.news_img" :src="form.news_img" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="内容:" prop="news_content">
        <div style="border: 1px solid #ccc; width: 800px">
          <Toolbar
            style="border-bottom: 1px solid #ccc"
            :editor="editor"
            :defaultConfig="toolbarConfig"
            :mode="mode"
          />
          <Editor
            style="height: 220px; overflow-y: hidden"
            v-model="html"
            :defaultConfig="editorConfig"
            :mode="mode"
            @onCreated="onCreated"
          />
        </div>
      </el-form-item>
      <el-form-item label="时间:" prop="news_time">
        <el-date-picker type="datetime" placeholder="选择日期时间"  v-model="form.news_time" value-format="yyyy-MM-dd HH:mm:ss">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onsubmit">点击新增</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import httpApi from '@/http';
export default {
  components: { Editor, Toolbar },
  data() {
    return {
      editor: null,
      html: "<p>要写点什么呢</p>",
      toolbarConfig: {},
      editorConfig: {
        placeholder: "请输入内容...",
        MENU_CONF: {
          uploadImage: {
            fieldName: "file", // 上传参数所使用的的字段名
            server: this.UPLOADURL + "/upload", // 上传目标路径
            customInsert(res, insertFn) {
              // 当上传成功后, 自动调用, 传入响应对象 res
              let url = res.data; // 上传成功的图片 的访问地址
              insertFn(url); // 自动把上传成功的图片, 显示在编辑器中
            },
          },
        },
      },
      mode: "simple",
      form: {
        news_title: "",
        news_img: "",
        news_content: "",
        news_time:''
      },
      rules: {
        news_title: [{ required: true, message: "必填", trigger: "blur" }],
        news_img: [{ required: true, message: "必填", trigger: "blur" }],
        news_content: [{ required: true, message: "必填", trigger: "blur" }],
        news_time: [{ required: true, message: "必填", trigger: "blur" }]
      },
    };
  },
  methods: {
    onCreated(editor) {
      // 配置上传文件相关参数
      this.editor = Object.seal(editor);
    },
    handleAvatarSuccess(res, file) {
      console.log(res);
      this.form.news_img = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    onsubmit() {
      this.form.news_content = this.editor.getText();
      console.log(this.form);
      this.$refs.form.validate((valid) => {
        if (valid) {
          httpApi.newsApi.add(this.form).then((res) => {
            console.log("新增新闻", res);
            // 跳转到列表页面
            this.$router.push("/home/news-list");
          });
        }
      });
    },
  },
};
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;

  font-size: 28px;
  color: #8c939d;
  width: 128px;
  height: 128px;
  line-height: 128px;
  text-align: center;
}
.avatar {
  width: 128px;
  display: block;
}
</style>